<template>
	<view class="content">
		<view class="modify">
			<!-- 登录密码 -->
			<template v-if="type == '1'">
				<view class="a_input">
					<text>原密码</text>
					<input v-model="resetPwd.oldPwd" class="uni-input" type="password" value="" placeholder-class="uni-place" placeholder="请输入原密码" />
				</view>
				<view class="a_input">
					<text>新密码</text>
					<input class="uni-input" v-model="resetPwd.newPwd" type="password" value="" placeholder-class="uni-place" placeholder="请输入新密码" />
				</view>
				<view class="a_input">
					<text>新密码确认</text>
					<input class="uni-input" v-model="resetPwd.cenPwd" type="password" value="" placeholder-class="uni-place" placeholder="请再次输入新密码" />
				</view>
				<view class="a_input" v-if="GOOGLE.status == 'ON'">
					<text>谷歌验证码</text>
					<input class="uni-input" type="password" v-model="resetPwd.google" value="" placeholder-class="uni-place" placeholder="请输入谷歌验证码" />
				</view>
				<view class="submit" @click="resetPwdSubmit()">提交</view>
			</template>
			<!-- 资金密码 -->
			<template v-if="type == '2'">
				<view class="a_input" v-if="USERINFO.setPayPassword">
					<text>原资金密码</text>
					<input class="uni-input" v-model="resetPayPwd.oldPwd" type="password" value="" placeholder-class="uni-place" placeholder="请输入原密码" />
				</view>
				<view class="a_input">
					<text>新资金密码</text>
					<input class="uni-input" v-model="resetPayPwd.newPwd" type="password" value="" placeholder-class="uni-place" placeholder="请输入新密码(6-20个字符)" />
				</view>
				<view class="a_input">
					<text>新密码确认</text>
					<input class="uni-input" v-model="resetPayPwd.cenPwd" type="password" value="" placeholder-class="uni-place" placeholder="请确认新密码" />
				</view>
				<view class="a_input">
					<text>验证码</text>
					<input class="uni-input"  type="tel" v-model="resetPayPwd.code" maxlength="6"  placeholder-class="uni-place" placeholder="请输入验证码" />
					<view v-if="numTime == false" class="send" @click="getCode()"><button plain>发送验证码</button></view>
					<view v-else class="send">
						<button plain disabled>{{ numText }}</button>
					</view>
				</view>
				<view class="a_input" v-if="GOOGLE.status == 'ON'">
					<text>谷歌验证码</text>
					<input class="uni-input" type="password" v-model="resetPayPwd.google" value="" placeholder-class="uni-place" placeholder="请输入谷歌验证码" />
				</view>
				<view class="submit" @click="resetPayPwdSubmit()">提交</view>
			</template>
			<!-- 邮箱 -->
			<template v-if="type == '3'">
				<view class="a_input">
					<text>邮箱地址</text>
					<input class="uni-input" v-model="email" type="text" value="" placeholder-class="uni-place" placeholder="请输入邮箱地址" />
				</view>
				<view class="a_input">
					<text>邮箱验证码</text>
					<input class="uni-input" type="tel" v-model="emailCode" maxlength="6" placeholder-class="uni-place" placeholder="请输入邮箱验证码" />
					<view v-if="numTime == false" class="send" @click="getCode()"><button plain>发送验证码</button></view>
					<view v-else class="send">
						<button plain disabled>{{ numText }}</button>
					</view>
				</view>
				<view class="submit" @click="resetPayPwdSubmit()">提交</view>
			</template>
		</view>
		 <HMmessages ref="HMmessages" @complete="HMmessages = $refs.HMmessages"></HMmessages>
	</view>
</template>

<script>
import uniIcons from '@/components/uni-icon/uni-icon.vue';
import { REG_EMAIL, REG_PHONE } from '@/common/reg.js';
import HMmessages from '@/components/HM-messages/HM-messages.vue';
import comSvc from '@/common/comSvc.js';
import service from '../service.js';
import { mapGetters } from 'vuex';
export default {
	components: { uniIcons,HMmessages },
	data() {
		return {
			type: '',
			numTime: false,
			numText: '60秒',
			num: 60,
			resetPwd: {
				oldPwd: '',
				newPwd: '',
				cenPwd: '',
				google: ''
			},
			resetPayPwd: {
				newPwd: '',
				cenPwd: '',
				code: '',
				oldPwd: '',
				google: '',
				idNumber: ''
			},
			email: '',
			emailCode: '',
		};
	},
	computed: {
		...mapGetters(['GOOGLE', 'AVOID', 'USERINFO'])
	},
	onShow() {},
	onLoad(option) {
		if (option) {
			this.type = option.type;
			if (option.type == '1') {
				uni.setNavigationBarTitle({
					title: '修改密码'
				});
			}
			if (option.type == '2') {
				if (this.USERINFO.setPayPassword) {
					uni.setNavigationBarTitle({
						title: '修改资金密码'
					});
				} else {
					uni.setNavigationBarTitle({
						title: '设置资金密码'
					});
				}
			}
			if (option.type == '3') {
				uni.setNavigationBarTitle({
					title: '邮箱验证'
				});
			}
		}
	},
	methods: {
		//获取验证码倒计时
		times() {
			let that = this;
			clearInterval(this.timeInt);
			this.timeInt = setInterval(() => {
				that.num--;
				if (that.num == 0) {
					clearInterval(that.time);
					that.numTime = false;
					that.num = 60;
					that.numText = that.num + '秒';
				} else {
					that.numText = that.num + '秒';
					that.times();
				}
			}, 1000);
		},
		//获取验证码
		getCode() {
			let type = '';
			let account = '';
			if(this.type == 3) {
				 type = 'EMAIL';
				 account = this.email
			} else {
				 type = 'PHONE';
				 account = this.USERINFO.account
			}
			
			
			this.$api.showLoading();
			comSvc
				.getCode({
					sendType: type,
					sendAccount: account
				})
				.then(({ data }) => {
					this.$api.hideLoading();
					if (data.code == '100200') {
						this.numTime = true;
						this.times();
						uni.showToast({
							title:'验证码已发送',
							icon:'none'
						})
					} else {
						uni.showToast({
							title: data.msg,
							icon: 'none'
						});
					}
				});
		},
		//修改登录密码
		resetPwdSubmit() {
			if (!this.resetPwd.oldPwd) {
				uni.showToast({
					title: '请输入原密码',
					duration: 2000
				});
				return false;
			}
			if (!this.resetPwd.newPwd) {
				uni.showToast({
					title: '请输入新密码',
					icon: 'none'
				});
				return false;
			}
			if (this.resetPwd.newPwd.length < 6 || this.resetPwd.newPwd.length > 20) {
				uni.showToast({
					title: '密码长度为6-20个字符',
					icon: 'none'
				});
				return false;
			}
			if (!this.resetPwd.cenPwd || this.resetPwd.cenPwd != this.resetPwd.newPwd) {
				uni.showToast({
					title: '密码输入不一致，请重新输入',
					icon: 'none'
				});
				return false;
			}
			let header = {
				'login-password': this.resetPwd.oldPwd
			};
			if (this.GOOGLE.status == 'ON' && !this.resetPwd.google) {
				uni.showToast({
					title: '请输入谷歌验证码',
					icon: 'none'
				});
				return false;
			} else {
				header['google-code'] = this.resetPwd.google;
			}
			this.$api.showLoading();
			service
				.resetPwd(
					{
						loginPassword: this.resetPwd.cenPwd
					},
					header
				)
				.then(({ data }) => {
					this.$api.hideLoading();
					if (data.code == '100200') {
						this.HMmessages.show('修改密码成功', {
							textAlign: 'center',
							background: '#356e36',
							fontColor: '#fff',
							callback: () => {
								uni.removeStorageSync('LOGIN_TIME');
								uni.removeStorageSync('TOKEN');
								uni.reLaunch({
									url: '../../login/login'
								});
							}
						});
					} else {
						if (data.code == '105100') {
							this.HMmessages.show('密码错误已达10次账户被锁定，请找回登录密码', {
								textAlign: 'center',
								background: '#74272d',
								fontColor: '#fff',
								callback: () => {
									uni.reLaunch({
										url: '../../login/login'
									});
								}
							});
						} else if (data.code == '104103') {
							uni.showToast({
								title: '原密码错误',
								icon: 'none'
							});
							this.errors('原密码错误');
						} else {
							uni.showToast({
								title: data.msg,
								icon: 'none'
							});
						}
					}
				});
		},
		//错误提示
		errors(text) {
		  this.HMmessages.show(text, {
		    textAlign: 'center',
		    background: '#74272d',
		    fontColor: '#fff'
		  });
		},
		//设置邮箱
		setEmail() {
		  if (!this.email) {
		    this.errors('请输入邮箱');
		    return false;
		  }
		  if (!Number(this.email)) {
		    this.errors('请输入正确的邮箱地址');
		    return false;
		  }
		  if (this.values == 0 && !REG_EMAIL.test(this.email)) {
		    this.errors('请输入正确的邮箱地址');
		    return false;
		  }
		  if (!this.emailCode) {
		    this.errors('请输入验证码');
		    return false;
		  }
		  this.$api.showLoading()
		  service
		  .setEmail({
		    email: this.addressList[this.values].tel + this.email
		  }, {
		    'service-code': this.emailCode
		  })
		  .then(({
		    data
		  }) => {
		    this.$api.hideLoading()
		    let that = this;
		    if (data.code == '100200') {
		      that.email = '';
		      that.emailCode = '';
		      that.$store.dispatch('getBasic');
		      this.HMmessages.show('绑定成功', {
		        textAlign: 'center',
		        background: '#356e36',
		        fontColor: '#fff'
		      });
		    } else {
		      this.errors(data.msg);
		    }
		  });
		},
		
		//修改或者设置资金密码
		resetPayPwdSubmit() {
			if (this.USERINFO.setPayPassword && !this.resetPayPwd.oldPwd) {
				uni.showToast({
					title: '请输入原密码',
					icon: 'none'
				});
				return false;
			}
			if (!this.resetPayPwd.newPwd) {
				uni.showToast({
					title: '请输入新密码',
					icon: 'none'
				});
				return false;
			}
			if (this.resetPayPwd.newPwd.length < 6 || this.resetPayPwd.newPwd.length > 20) {
				uni.showToast({
					title: '密码长度为6-20个字符',
					icon: 'none'
				});
				return false;
			}
			if (!this.resetPayPwd.cenPwd || this.resetPayPwd.cenPwd != this.resetPayPwd.newPwd) {
				uni.showToast({
					title: '密码输入不一致，请重新输入',
					icon: 'none'
				});
				return false;
			}
			if (!this.resetPayPwd.code) {
				uni.showToast({
					title: '请输入验证码',
					icon: 'none'
				});
				return false;
			}
			if (this.USERINFO.setPayPassword && this.GOOGLE.status == 'ON' && !this.resetPayPwd.google) {
				uni.showToast({
					title: '请输入谷歌验证码',
					icon: 'none'
				});
				return false;
			}
			let params = {
				payPassword: this.resetPayPwd.cenPwd
			};
			let header = {
				'service-code': this.resetPayPwd.code
			};
			let name = '';
			if (this.USERINFO.setPayPassword) {
				name = 'changePayPwd';
				header['pay-password'] = this.resetPayPwd.oldPwd;
				if (this.GOOGLE.status == 'ON') {
					header['google-code'] = this.resetPayPwd.google;
				}
			} else {
				name = 'resetPayPwd';
			}
			this.$api.showLoading();
			service[name](params, header).then(({ data }) => {
				this.$api.hideLoading();
				let that = this;
				if (data.code == '100200') {
					that.$store.dispatch('getBasic');
					that.resetPayPwd = {
						newPwd: '',
						cenPwd: '',
						code: '',
						oldPwd: '',
						google: '',
						idNumber: ''
					};
					this.HMmessages.show('修改成功', {
						textAlign: 'center',
						background: '#356e36',
						fontColor: '#fff'
					});
				} else {
					
					this.errors(data.msg);
				}
			});
		}
	}
};
</script>

<style lang="scss" scoped>
@import './style.scss';
</style>
